package com.fz.study.project;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fz.study.project.entity.Result;
import com.fz.study.project.entity.dto.LoginDto;
import com.fz.study.project.entity.dto.LoginFormDTO;
import com.fz.study.project.service.impl.ShopServiceImpl;
import com.fz.study.project.service.impl.UserServiceImpl;
import com.fz.study.project.utils.RedisIdWorker;
import lombok.RequiredArgsConstructor;
import okio.Path;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.swing.text.Utilities;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootTest
public class RedisDemo {
    //    自动的序列化类，应用之后就不需要再配置序列化了
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RedisIdWorker redisIdWorker;

    @Autowired
    private UserServiceImpl userService;

    private ExecutorService es = Executors.newFixedThreadPool(500);

    @Test
    void redisTest() {
//        写入
        redisTemplate.opsForValue().set("name", "阿正");
//        查询
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }

    @Test
    void userTest() {
        LoginDto user = new LoginDto("lisi", "123456");
//        json转字符串
        String StringUser = JSON.toJSONString(user);
//        写数据
        redisTemplate.opsForValue().set("fzstudy:user:3", StringUser);
//        读数据
        String o = redisTemplate.opsForValue().get("fzstudy:user:2");
//        字符串转json
        Object json = JSON.toJSON(o);
        System.out.println(json);
    }

    @Test
    void testIdWorker() throws InterruptedException {
//        倒计时插销
        CountDownLatch latch = new CountDownLatch(300);
        Runnable task = () -> {
            for (int i = 0; i < 100; i++) {
                long id = redisIdWorker.nextId("order");
                System.out.println("id=" + id);
            }
//            执行完一次就cuntdown一次
            latch.countDown();
        };
        long begin = System.currentTimeMillis();
        for (int i = 0; i < 300; i++) {
            es.submit(task);
        }
//        等待countDown结束
        latch.await();
        long end = System.currentTimeMillis();
        System.out.println("time = " + (end - begin));
    }

//    生成批量的用户信息并保存到数据库，并将token信息输出到filename.txt文件
    @Test
    void creatToken() throws IOException {
        LoginFormDTO loginFormDTO = new LoginFormDTO();
        FileOutputStream fos = new FileOutputStream("D://filename.txt");
        String phone = "13111111111";
        for (int i = 0; i < 1000; i++) {
            long intPhone = Long.parseLong(phone);
            intPhone++;
            String s = String.valueOf(intPhone);
            loginFormDTO.setPhone(s);
            phone = s;
            Result login = userService.login(loginFormDTO);
            byte[] str = login.getData().toString().getBytes();
            fos.write(str);
            fos.write(System.lineSeparator().getBytes());//换行
        }
    }

}
